Example Analysis of PHP Observer Pattern [Contrast JS Observer Pattern]

  • 2021-12-11 07:01:32
  • OfStack

This article illustrates the PHP observer pattern. Share it for your reference, as follows:

1. Implement the Observer Pattern with js


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
    div{width: 100px;height: 100px;border: 1px #999 solid;margin-bottom: 5px;}
    </style>
</head>
<body>
<!--
 We let div Object observation select The changes, selecte Changes will inform this 2 Object, and causes this 2 Object changes, realizing the observer pattern. 
 -->
 <h1> Switch page style with observer mode </h1>
 <select>
     <option value="male"> Men's style </option>
     <option value="female"> Lady style </option>
 </select>
 <button onclick="t1()"> Observation learning area </button>
 <button onclick="t2()"> Do not observe the learning area </button>
 <div id="content"> I am the content </div>
 <div id="ad"> I am an advertisement </div>
 <div id="study"> Learning </div>
</body>
<script type="text/javascript">
    var sel = document.getElementsByTagName('select')[0];
    sel.observers = {};
    sel.attach = function(key,obj){
        this.observers[key] = obj;
    }
    sel.detach = function(key){
        delete this.observers[key];
    }
    sel.onchange = sel.notify = function(){
        for(var key in this.observers){
            this.observers[key].update(this);
        }
    }
    // Client 
    var content = document.getElementById('content');
    var ad = document.getElementById('ad');
    content.update = function(ob){
        if (ob.value == 'male') {
            this.style.backgroundColor = 'gray';
        }else if(ob.value == 'female'){
            this.style.backgroundColor = 'pink';
        }
    }
    ad.update = function(ob){
        if (ob.value == 'male') {
            this.innerHTML = ' Automobile ';
        }else if(ob.value == 'female'){
            this.innerHTML = ' Lose weight ';
        }
    }
    // Jean content Observation select Change of 
    sel.attach('content',content);
    sel.attach('ad',ad);
    // Add monitoring study District 
    var study = document.getElementById('study');
    study.update = function(ob){
        if (ob.value == 'male') {
            this.innerHTML = ' Learning computer ';
        }else if(ob.value == 'female'){
            this.innerHTML = ' Learn beauty ';
        }
    }
    sel.attach('study',study);
    function t1(){
        sel.attach('study',study);
    }
    function t2(){
        sel.detach('study');
    }
</script>
</html>

2. Implement observation mode with php


<?php
//php Realization observer 
//php5 Provide observers in observer And the observed subject Interface of 
class User implements SplSubject
{
    public $lognum;
    public $hobby;
    protected $observers = null;
    public function __construct($hobby)
    {
        $this->lognum = rand(1,10);
        $this->hobby = $hobby;
        $this->observers = new SplObjectStorage();
    }
    public function login()
    {
        // Operation session Etc 
        $this->notify();
    }
    public function attach(SPLObserver $observer)
    {
        $this->observers->attach($observer);
    }
    public function detach(SPLObserver $observer)
    {
        $this->observers->detach($observer);
    }
    public function notify()
    {
        $this->observers->rewind();
        while ($this->observers->valid()) {
            $observer = $this->observers->current();
            $observer->update($this);
            $this->observers->next();
        }
    }
}
// User Secure Login Module 
class Safe implements SPLObserver
{
    public function update(SplSubject $subject)
    {
        if ($subject->lognum < 3) {
            echo ' This is the first ' . $subject->lognum . ' Sub-secure login <br>';
        }else{
            echo ' This is the first ' . $subject->lognum . ' Secondary login, exception <br>';
        }
    }
}
// Advertising module 
class Ad implements SPLObserver
{
    public function update(SplSubject $subject)
    {
        if ($subject->hobby == 'sports') {
            echo ' The Premier League has begun <br>';
        }else{
            echo ' Study hard <br>';
        }
    }
}
// Implementation observation 
// $user = new User('sports');
$user = new User('study');
$user->attach(new Safe());
$user->attach(new Ad());
$user->login();// Login 

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: